import { ref } from 'vue'
import { goDialog } from '@/utils'
import { DialogEnum } from '@/enums/pluginEnum'
import { ChartList } from '../../..'
import { projectAdd,  projectQuery, projectDelete } from '@/api/screen'
import { getLocalStorage } from '@/utils'
import { random } from 'lodash'
// 数据初始化
export const useDataListInit = () => {
  const list = ref<ChartList>([])
  const current = ref(1)
  const total = ref(0)

  projectQuery({
    // pageNum: current.value,
    // pageSize: 20
  }).then(res => {
    if (res.code === 200) {
      const data = res.data

      list.value = data.map(item => {
        return {
          id: item.sid,
          title: item.pageName,
          release: item.publishState,
          label: item.pageName,
          indexImage: item.indexImage
        }
      })

      console.log('list', list.value)
    } else {
      window.$message.error('获取项目列表失败')
    }
  })
  // const list = ref<ChartList>([
  //   {
  //     id: 1,
  //     title: '物料1-假数据不可用',
  //     release: true,
  //     label: '官方案例'
  //   },
  //   {
  //     id: 2,
  //     title: '物料2-假数据不可用',
  //     release: false,
  //     label: '官方案例'
  //   },
  //   {
  //     id: 3,
  //     title: '物料3-假数据不可用',
  //     release: false,
  //     label: '官方案例'
  //   },
  //   {
  //     id: 4,
  //     title: '物料4-假数据不可用',
  //     release: false,
  //     label: '官方案例'
  //   },
  //   {
  //     id: 5,
  //     title: '物料5-假数据不可用',
  //     release: false,
  //     label: '官方案例'
  //   }
  // ])

  // 删除
  const deleteHandle = (cardData: object, index: number) => {
    goDialog({
      type: DialogEnum.DELETE,
      promise: true,
      onPositiveCallback: () => {
        projectDelete({
          sid: cardData['id']
        }).then(res => {
          if (res.code === 200) {
            window.$message.success('删除成功')
            useDataListInit()
            list.value.splice(index, 1)
          } else {
            window.$message.error('删除失败')
          }
        })
      }
      // promiseResCallback: (e: any) => {
      //   window.$message.success('删除成功')
      //   list.value.splice(index, 1)
      // }
    })
  }
  const addHandle = (cardData: object) => {
    projectAdd(cardData).then(res => {
      if (res.code === 200) {
        window.$message.success('新增成功')
        useDataListInit()
      } else {
        window.$message.error(res.msg)
      }
    })
  }

  return {
    list,
    current,
    total,
    deleteHandle
  }
}
